import CellType from '../celltypes';

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const FieldCellType = CellType('FieldCellType');
        const { sheet, range, _oldTag, _oldCellType, _oldTextIndent, _oldBackgroundImageLayout, _oldBackgroundImage, cellType } = options;
        sheet.suspendPaint();
        if (isUndo) {
            range.tag(_oldTag)
                .cellType(_oldCellType ? new FieldCellType().fromJSON(_oldCellType) : new GC.Spread.Sheets.CellTypes.Base())
                .textIndent(_oldTextIndent)
                .backgroundImageLayout(_oldBackgroundImageLayout)
                .backgroundImage(_oldBackgroundImage);

        } else {
            const ct = range.cellType();
            if (ct) {
                options._oldCellType = ct.toJSON();
            }
            options._oldTextIndent = range.textIndent();
            options._oldBackgroundImageLayout = range.backgroundImageLayout();
            options._oldBackgroundImage = range.backgroundImage();
            options._oldTag = range.tag();
            range.bindingPath(cellType.fieldName)
                .tag('MainField')
                .cellType(cellType)
                .textIndent(2.5)
                .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                .backgroundImage(cellType.getFieldTypeImage());
        }
        sheet.resumePaint();
    }
};